using System;

using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using System.Threading.Tasks;

namespace FlyOutMenu
{
	[Activity (Label = "FlyInMenu", MainLauncher = true, Theme = "@android:style/Theme.Holo.Light.NoActionBar")]
	public class Activity1 : Activity
	{

		ProgressDialog progress;
		string strResult = string.Empty;

		protected override void OnCreate (Bundle bundle)
		{
			base.OnCreate (bundle);

			// Set our view from the "main" layout resource
			SetContentView (Resource.Layout.Main);

			var txtv = FindViewById<TextView>(Resource.Id.txtvResult);
			var btn = FindViewById<Button>(Resource.Id.btnWS);

			TextView btn1 = FindViewById<TextView> (Resource.Id.button1);
			TextView btn2 = FindViewById<TextView> (Resource.Id.button2);
			var cont1 = FindViewById<LinearLayout> (Resource.Id.content1);
			var cont2 = FindViewById<LinearLayout> (Resource.Id.content2);
			cont2.Visibility=ViewStates.Visible;
			cont1.Visibility=ViewStates.Gone;

			var menu = FindViewById<FlyOutContainer> (Resource.Id.FlyOutContainer);
			var menuButton = FindViewById (Resource.Id.MenuButton);
			menuButton.Click += (sender, e) => {
				menu._boolFlag=false;
				menu.AnimatedOpened = !menu.AnimatedOpened;
			};
			btn1.Click += delegate {
				if (cont1.Visibility!=ViewStates.Visible) {
					menu._boolFlag = true;
					cont2.Visibility = ViewStates.Gone;
					cont1.Visibility = ViewStates.Visible;
					txtv.Text=string.Empty;
				}
				menu.AnimatedOpened = !menu.AnimatedOpened;
			};
			btn2.Click += delegate {
				if (cont2.Visibility!=ViewStates.Visible) {
					menu._boolFlag = true;
					cont2.Visibility = ViewStates.Visible;
					cont1.Visibility = ViewStates.Gone;
				}
				menu.AnimatedOpened = !menu.AnimatedOpened;
			}; 

			//
		
			btn.Click += delegate{

				// show the loading overlay on the UI thread
				progress = ProgressDialog.Show(this, "Loading WS", "Please Wait...", true); 
				//txtv1.Text="Hello from taskA.";
				// spin up a new thread to do some long running work using StartNew
				Task.Factory.StartNew (
					// tasks allow you to use the lambda syntax to pass work
					() => {
						//Begin the task
						strResult=RunWS();
					}

				).ContinueWith ( //Do something whene finish the task
					t => {
						if (progress != null)
							progress.Hide();

						//Show the result at the end of the task
						txtv.Text=strResult;

					}, TaskScheduler.FromCurrentSynchronizationContext()
				);

				// Output a message from the original thread. note that this executes before
				// the background thread has finished.
				//Console.WriteLine("Hello from the calling thread.");
				//txtv1.Text="Hello from the calling thread.";

			};

		}

		/// <summary>
		/// Run webService
		/// This is a webservice 2.0 not a wcf; something is diffrent
		/// </summary>
		/// <param name="seconds">Seconds.</param>
		protected string RunWS ()
		{
			wsConvert.TempConvert ws = new FlyOutMenu.wsConvert.TempConvert ();
			string strReturn = ws.CelsiusToFahrenheit ("21");
			return strReturn;
		}
	}
}


